<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }

        /* 导航栏样式 */
        .nav {
            background-color: #2c3e50;
            padding: 15px 0;
            margin-bottom: 30px;
            text-align: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            position: relative;
        }

        .nav a {
            color: white;
            text-decoration: none;
            margin: 0 15px;
            padding: 8px 15px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .nav a:hover {
            background-color: #34495e;
            border-radius: 4px;
        }

        .nav a.active {
            background-color: #3498db;
            border-radius: 4px;
        }

        /* 退出按钮样式 */
        .logout-btn {
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            background-color: #e74c3c;
            color: white;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        /* 内容区域样式 */
        .container {
            width: 80%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        /* 表单样式 */
        .search-form {
            margin-bottom: 20px;
            text-align: center;
        }

        .search-form input[type="text"] {
            padding: 8px 15px;
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            font-size: 14px;
        }

        .search-form input[type="submit"],
        button {
            padding: 8px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s ease;
        }

        .search-form input[type="submit"]:hover,
        button:hover {
            background-color: #2980b9;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-top: 20px;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #2c3e50;
            color: white;
            font-weight: normal;
        }

        tr:hover {
            background-color: #f9f9f9;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 25px;
            border-radius: 5px;
            width: 50%;
            max-width: 500px;
            box-shadow: 0 3px 6px rgba(0,0,0,0.2);
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: color 0.3s ease;
        }

        .close:hover {
            color: #2c3e50;
        }

        /* 表单元素样式 */
        .modal-content form {
            margin-top: 20px;
        }

        .modal-content label {
            display: block;
            margin-bottom: 8px;
            color: #2c3e50;
        }

        .modal-content input[type="text"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .modal-content input[type="submit"] {
            background-color: #3498db;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            font-size: 14px;
        }

        .modal-content input[type="submit"]:hover {
            background-color: #2980b9;
        }

        /* 操作链接样式 */
        .action-link {
            color: #3498db;
            text-decoration: none;
            margin: 0 5px;
            transition: color 0.3s ease;
        }

        .action-link:hover {
            color: #2980b9;
        }

        .delete-link {
            color: #e74c3c;
        }

        .delete-link:hover {
            color: #c0392b;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<div class="nav">
    <a href="index.html">学生管理</a>
    <a href="user.html">用户管理</a>
    <a href="major.html" class="active">专业管理</a>
    <a href="javascript:void(0)" class="logout-btn" onclick="logout()">退出登录</a>
</div>

<div class="container">
    <div class="search-form">
        <form id="searchForm" action="major/query" method="GET">
            <input type="text" name="name" placeholder="请输入专业名称" value="">
            <input type="submit" value="查询">
        </form>
        <p>
            <button id="addButton">添加专业</button>
        </p>
    </div>

    <table id="majorTable">
        <thead>
            <tr>
                <th>专业编号</th>
                <th>专业名称</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- 这里会动态填充专业信息 -->
        </tbody>
    </table>
</div>

<!-- 编辑模态框 -->
<div id="editModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>编辑专业信息</h2>
        <form id="editForm">
            <input type="hidden" id="editId">
            <label for="editName">专业名称:</label>
            <input type="text" id="editName" required>
            <input type="submit" value="保存">
        </form>
    </div>
</div>

<!-- 添加专业模态框 -->
<div id="addModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>添加专业信息</h2>
        <form id="addForm">
            <label for="addName">专业名称:</label>
            <input type="text" id="addName" required>
            <input type="submit" value="保存">
        </form>
    </div>
</div>

<script>
    window.onload = function() {
        // 添加退出登录函数
        window.logout = function() {
            if (confirm('确定要退出登录吗？')) {
                window.location.href = 'login.html';
            }
        };

        // 加载专业列表
        function loadMajors(searchName) {
            var xhr = new XMLHttpRequest();
            var url = 'major/query';
            if (searchName) {
                url += '?name=' + encodeURIComponent(searchName);
            }
            console.log('发送请求到:', url); // 调试日志

            xhr.open('GET', url, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    console.log('收到响应:', xhr.status, xhr.responseText); // 调试日志
                    if (xhr.status === 200) {
                        try {
                            var majors = JSON.parse(xhr.responseText);
                            var table = document.getElementById('majorTable');
                            var tbody = table.getElementsByTagName('tbody')[0];
                            tbody.innerHTML = ''; // 清空现有内容

                            majors.forEach(function(major) {
                                var row = document.createElement('tr');
                                row.innerHTML = `
                                    <td>${major.id}</td>
                                    <td>${major.name}</td>
                                    <td>
                                        <a href="#" class="action-link edit-link" data-id="${major.id}">编辑</a>
                                        <a href="#" class="action-link delete-link" data-id="${major.id}">删除</a>
                                    </td>
                                `;
                                tbody.appendChild(row);
                            });
                            addEditDeleteListeners();
                        } catch (e) {
                            console.error('解析数据失败:', e);
                            alert('解析数据失败，请重试');
                        }
                    } else {
                        alert("查询失败：" + xhr.responseText);
                    }
                }
            };
            xhr.send();
        }

        // 页面加载时获取专业列表
        loadMajors();

        // 搜索表单提交
        var searchForm = document.getElementById('searchForm');
        console.log('搜索表单元素:', searchForm); // 调试日志

        if (searchForm) {
            searchForm.addEventListener('submit', function(e) {
                e.preventDefault();
                console.log('表单提交事件触发'); // 调试日志
                
                var nameInput = document.querySelector('#searchForm input[name="name"]');
                console.log('搜索输入框:', nameInput); // 调试日志
                
                var searchName = nameInput ? nameInput.value.trim() : '';
                console.log('搜索关键词:', searchName); // 调试日志
                
                loadMajors(searchName);
            });
        } else {
            console.error('未找到搜索表单元素!'); // 调试日志
        }

        function addEditDeleteListeners() {
            var editLinks = document.querySelectorAll('.edit-link');
            var deleteLinks = document.querySelectorAll('.delete-link');

            editLinks.forEach(function(link) {
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    var id = this.getAttribute('data-id');
                    var row = this.closest('tr');
                    var name = row.cells[1].textContent;

                    document.getElementById('editId').value = id;
                    document.getElementById('editName').value = name;

                    var modal = document.getElementById('editModal');
                    modal.style.display = "block";
                });
            });

            deleteLinks.forEach(function(link) {
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    var id = this.getAttribute('data-id');
                    if (confirm('确定要删除此专业吗？')) {
                        var xhrDelete = new XMLHttpRequest();
                        xhrDelete.open('GET', 'major/delete?id=' + id, true);
                        xhrDelete.onreadystatechange = function() {
                            if (xhrDelete.readyState === 4) {
                                if (xhrDelete.status === 200) {
                                    loadMajors();
                                } else {
                                    alert('删除失败：' + xhrDelete.responseText);
                                }
                            }
                        };
                        xhrDelete.send();
                    }
                });
            });
        }

        // 添加专业按钮点击事件
        document.getElementById('addButton').addEventListener('click', function() {
            var modal = document.getElementById('addModal');
            modal.style.display = "block";
        });

        // 关闭模态框
        var spans = document.getElementsByClassName("close");
        Array.from(spans).forEach(function(span) {
            span.onclick = function() {
                this.closest('.modal').style.display = "none";
            }
        });

        // 提交编辑表单
        document.getElementById('editForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var id = document.getElementById('editId').value;
            var name = document.getElementById('editName').value;

            var xhrEdit = new XMLHttpRequest();
            xhrEdit.open('POST', 'major/edit', true);
            xhrEdit.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhrEdit.onreadystatechange = function() {
                if (xhrEdit.readyState === 4) {
                    if (xhrEdit.status === 200) {
                        document.getElementById('editModal').style.display = "none";
                        loadMajors();
                    } else {
                        alert("修改失败：" + xhrEdit.responseText);
                    }
                }
            };
            xhrEdit.send('id=' + encodeURIComponent(id) + '&name=' + encodeURIComponent(name));
        });

        // 提交添加表单
        document.getElementById('addForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var name = document.getElementById('addName').value;

            var xhrAdd = new XMLHttpRequest();
            xhrAdd.open('POST', 'major/add', true);
            xhrAdd.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhrAdd.onreadystatechange = function() {
                if (xhrAdd.readyState === 4) {
                    if (xhrAdd.status === 200) {
                        document.getElementById('addModal').style.display = "none";
                        document.getElementById('addForm').reset();
                        loadMajors();
                    } else {
                        alert("添加失败：" + xhrAdd.responseText);
                    }
                }
            };
            xhrAdd.send('name=' + encodeURIComponent(name));
        });
    };
</script>
</body>
</html>
